package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {


    //将转换后的数据放入数据库中的方法,并根据日期判断进行添加还是修改操作
    @Override
    public void importOrderSettings(List<String[]> orderSettingList) {

        //先将一个String数组的list集合转换为一个OrderSetting对象的list集合
        List<OrderSetting> orderSettingImportList = changeStringArrayToList(orderSettingList);

        //这时可以通过查询表中的日期是否存在来判断进行添加还是修改操作

        //1.遍历orderSettingImportList集合,拿到集合中的orderSetting对象
        //  若此对象在数据库中存在,则对ID进行重新赋值,进行修改操作,否则进行添加操作
        for (OrderSetting orderSetting : orderSettingImportList) {
            Date orderDate = orderSetting.getOrderDate();
            //处理查询数据库中是否存在此日期
            //1.创建一个QueryWrapper对象
            QueryWrapper<OrderSetting> orderSettingQueryWrapper = new QueryWrapper<>();
            //2.设置查询条件,数据库中是否存在文件中的日期
            orderSettingQueryWrapper.eq("orderdate", orderDate);
            //3.查询,给条件,返回一个OrderSetting对象,此对象是与当前插入的数据日期一致的一个对象
            //  若此对象存在,则代表数据库中存在当前插入的这条数据,需要将ID进行赋值,进行修改操作
            //  否则,此对象不存在,需要进行添加操作
            OrderSetting orderSetttingExistOrNot = baseMapper.selectOne(orderSettingQueryWrapper);
            //4.判断此OrderSetting是否存在
            if (orderSetttingExistOrNot != null) {
                //此时说明数据库中存在此数据,需要进行修改操作,所以将数据库中此数据的ID赋值给需要插入的数据
                orderSetting.setId(orderSetttingExistOrNot.getId());
            }
        }
        //放入自带的方法进行添加或删除
        saveOrUpdateBatch(orderSettingImportList);
    }

    //根据年和月查询对应的orderSetting
    @Override
    public Map findSettingData(int year, int month) {
        //查询的开始时间以及结束时间
        String beginDate = year + "-" + month + "-1";
        String endDate = year + "-" + month + "-31";

        //创建一个方法用来查询,返回值是orderSetting的集合
        List<OrderSetting> orderSettingList = baseMapper.selectOrderSettingByYearAndMonth(beginDate, endDate);

        HashMap<String, OrderSetting> map = new HashMap<>();
        //遍历集合,将集合放入Map中
        for (OrderSetting orderSetting : orderSettingList) {
            //获取查询的时间
            Date orderDate = orderSetting.getOrderDate();
            //时间转换
            String date2String = DateUtils.parseDate2String(orderDate, "yyyy-MM-dd");

            //对数据进行简化
            OrderSetting orderSettingVO = new OrderSetting();
            orderSettingVO.setNumber(orderSetting.getNumber());
            orderSettingVO.setReservations(orderSetting.getReservations());

            map.put(date2String, orderSettingVO);
        }
        return map;
    }

    //修改数据库的最大预约人数
    @Override
    public Boolean updateOrdersettingNumberBySettingData(int number, String settingDay) {
        return baseMapper.updateOrdersettingNumberBySettingData(number, settingDay);
    }

    //根据日期查询预约人数是否已满
    @Override
    public int selectOrdersettingInfoByOrderDate(String orderDate) {
        return baseMapper.selectOrdersettingInfoByOrderDate(orderDate);
    }

    @Override
    @LcnTransaction
    public void updateOrdersettingInfoByOrderDate(String orderDate) {
        baseMapper.updateOrdersettingInfoByOrderDate(orderDate);
    }


    //String数组的第一个字段是日期Date(yyyy/MM/dd),第二个字段是最大预约人数number
    private List<OrderSetting> changeStringArrayToList(List<String[]> orderSettingList) {
        //先判断集合是否为空
        if (orderSettingList != null && orderSettingList.size() > 0) {
            List<OrderSetting> orderSettingsList = new ArrayList<>();
            //遍历取出数据
            for (String[] strings : orderSettingList) {
                //取出的是一个个字符串数组
                //先创建一个OrderSetting对象
                OrderSetting orderSetting = new OrderSetting();
                //初始放入预约人数为0
                orderSetting.setReservations(0);
                orderSetting.setOrderDate(DateUtils.parseString2Date(strings[0], "yyyy/MM/dd"));
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                orderSettingsList.add(orderSetting);
            }
            return orderSettingsList;
        }
        return null;
    }



    // xxl-job调度清理T_ORDERSETTING数据表方法
    @Override
    public void cleanOrderSettingJob(){
        //获取当前时间zhi
        Calendar cal = Calendar.getInstance();
        //调到上个月
        cal.add(Calendar.MONTH, -1);
        //得到一dao个月最最后一版天日期(31/30/29/28)
        int MaxDay=cal.getMaximum(Calendar.DAY_OF_MONTH);
        //按你的要求设置权时间
        cal.set( cal.get(Calendar.YEAR),
                cal.get(Calendar.MONTH), MaxDay);
        //按格式输出
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(cal.getTime());
        baseMapper.cleanOrderSettingByDate(date);
    }
}
